package dr.server.controller.demo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonObject;
import dr.common.vo.PusherMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * kafka 生成者
 *
 * @version 1.0
 * @className: KafkaProducerController
 **/
@Slf4j
@RestController
@RequestMapping("/kafka")
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    //访问：localhost:8086/kafka/send?msg=hello(消息)

    @RequestMapping("/sendSms/aliyun")
    public String sendSmsAliyun() {
        //构造数据
        PusherMessage message = new PusherMessage();
        message.setMsgId(IdUtil.objectId());
        message.setType("1");
        message.setTo("18680750256");
        message.setContent("SMS_167595014");
        message.setJsonParam("{\"code\": \"11111\"}");

        log.info("发送消息：{}", JSONUtil.toJsonStr(message));
        // 消息的主题 TEST_TOPIC
        kafkaTemplate.send("ZSKJ-pusher", JSONUtil.toJsonStr(message));
        return "success";
    }

    @RequestMapping("/sendSms/submail")
    public String sendSmsSubmail() {
        //构造数据
        PusherMessage message = new PusherMessage();
        message.setMsgId(IdUtil.objectId());
        message.setType("2");
        message.setTo("18680750256");
        message.setContent("发送短信哦");
        message.setJsonParam("");

        log.info("发送消息：{}", JSONUtil.toJsonStr(message));
        // 消息的主题 TEST_TOPIC
        kafkaTemplate.send("ZSKJ-pusher", JSONUtil.toJsonStr(message));
        return "success";
    }


    @RequestMapping("/sendPush")
    public String sendPush() {
        //构造数据 type,to,title,msgOrTemplate,jsonParam,cronTime,msgType

        ArrayList<String> alias = CollUtil.newArrayList();
        alias.add("ec9a6e8c141f40fab277c0ea519da6f0");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("url","magic://app/order/list?order=rush&index=1");

        PusherMessage message = new PusherMessage();
        message.setMsgId(IdUtil.objectId());
        message.setMsgType("1");
        message.setType("3");
        message.setTo(alias.toString().substring(1, alias.toString().length()-1));
        message.setTitle("极光推送");
        message.setContent("进入XXXX商城，更多惊喜等着你！");
        message.setJsonParam(jsonObject.toString());

        log.info("发送消息：{}", JSONUtil.toJsonStr(message));
        // 消息的主题 TEST_TOPIC
        kafkaTemplate.send("ZSKJ-pusher", JSONUtil.toJsonStr(message));
        return "success";
    }

    @RequestMapping("/sendEmail")
    public String sendEmail() {
        //构造数据
        PusherMessage message = new PusherMessage();
        message.setMsgId(IdUtil.objectId());
        message.setType("4");
        message.setTo("506953110@qq.com");
        message.setContent("邮件发送成功");
        message.setMsgType("1");

        log.info("发送消息：{}", JSONUtil.toJsonStr(message));
        // 消息的主题 TEST_TOPIC
        kafkaTemplate.send("ZSKJ-pusher", JSONUtil.toJsonStr(message));
        return "success";
    }
}